#!/usr/bin/python -itt

######### standard library imports ###########################################
import struct
import sys
import time 
from pprint import pprint
import logging
import logging.config
import os

######### external library imports ###########################################
import numpy as np
import matplotlib.pyplot as plt
from ROOT import *

import reflector

if len(sys.argv) == 3:
    theta = float(sys.argv[2])
    phi = float(sys.argv[1])
elif len(sys.argv) == 2:
    theta = float(sys.argv[1])
    phi = 0.
else:
    theta = 0.
    phi = 0.
    
theta *= np.pi/180.
phi  *= np.pi/180.

run = {}
run['photonData'] = {}
pd = run['photonData']
#make some test photons
N = 10000
pos = np.zeros( (N,3) )
dir = np.zeros( (N,3) )

x = np.sin(theta)*np.cos(phi)
y = np.sin(theta)*np.sin(phi)
z = np.cos(theta)

dir[:,0] = x
dir[:,1] = y
dir[:,2] = z

pos[:,0]=np.random.uniform(-300,300,size=N)
pos[:,1]=np.random.uniform(-300,300,size=N)
pd['pos'] = pos
pd['dir'] = dir

run = reflector.pushMirrorInfo( run )

run = reflector.findMirrorIntersection( run, dictname='photonData', 
        pos_key='pos', 
        dir_key='dir',
        mirror_key='mirrors',
        new_pos_key='pos_on_mirror', 
        new_mirror_id_key='mirror_id', 
        new_time_key='time_to_mirror')
        
print "---------- photons hit mirrors:", pd['pos'].shape[0]

run = reflector.reflectOnMirrors( run, dictname='photonData', 
        mirror_alignment_point_key='mirror_alignment_point',
        pos_key='pos_on_mirror', 
        dir_key='dir',
        new_angle_key='angle_on_mirror', 
        new_dir_key='dir_on_mirror' )
    
run = reflector.findFocalPlaneIntersection( run, dictname='photonData', 
        focal_plane_key='focal_plane',
        pos_key='pos_on_mirror', 
        dir_key='dir_on_mirror',
        new_pos_key='pos_in_focal_plane', 
        new_time_key='time_to_focal_plane')
        
run = reflector.findPixelChid( run, dictname='photonData', 
        focal_plane_key='focal_plane',
        pos_key='pos_in_focal_plane', 
        new_chid_key='chid', 
        new_Coordinator_key='Coordinator', 
        new_pixel_center_key='pixel_center')


import new_helpers as helpers
data = pd
#~ helpers.plot_in_TH2F( data['pos'][:,:-1], 'pos' )
#~ helpers.plot_in_TH2F( data['pos_on_mirror'][:,:-1], 'pos_on_mirror' )
#~ helpers.plot_in_TGraph2D( data['pos_on_mirror'] , 'pos_on_mirror as plot')
#~ helpers.plot_in_TGraph2D( data['dir'] ,'dir as plot')
#~ helpers.plot_in_TGraph2D( data['dir_on_mirror'] ,'dir_on_mirro')
#~ helpers.plot_in_TH1F( data['angle_on_mirror'] ,'angle_on_mirror')
#~ helpers.plot_in_TH2F( data['dir_on_mirror'][:,:-1] ,'dir_on_mirror')
helpers.plot_in_TH2F( data['pos_in_focal_plane'][:,:-1] ,'pos_in_focal_plane', bins=(21,21) )

import plotters
camplotter = plotters.CamPlotter("Pixel Center", dpi=80, s=80)

photons_in_pixel = np.zeros(1440)
for i in range(1440):
    photons_in_pixel[i] = (data['chid'] == i).sum()
    
camplotter(photons_in_pixel)
plt.xlim(-22,22)
plt.ylim(-22,22)
